<?php

function reequipment_init()
{
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    xarDBLoadTableMaintenanceAPI();
    
    $query = xarDBCreateTable($xartable['reareas'],
                             array('area_id'     => array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => true,
                                                            'primary_key'   => true),
                                   'area_name'   => array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => '')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $query = xarDBCreateTable($xartable['rel_eq'],
                             array('user_id'     => array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => false,
                                                            'primary_key'   => false),
                                   'company_id'  => array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => false,
                                                            'primary_key'   => false),
   								   'company_name'=> array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => '')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;

    // allow several entries for the same keyword here
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rel_eq'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $query = xarDBCreateTable($xartable['refrequency'],
                             array('frequency_id'   => array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => true,
                                                            'primary_key'   => true),
                                   'frequency'  	=> array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => '')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
 
    
    $query = xarDBCreateTable($xartable['remodels'],
                             array('model_id'     => array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => true,
                                                            'primary_key'   => true),
                                   'area_id'	  => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),
                                   'model_name'   => array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => true,
                                                              'default'     => ''),                   
                                   'current'      => array('type'         => 'boolean')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;

    // allow several entries for the same keyword here
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_area_id',
        'fields'    => array('area_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['remodels'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    
    $query = xarDBCreateTable($xartable['reequipment'],
                             array('equipment_id'   => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => true,
                                                             'primary_key'   => true),
                                   'model_id'       	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),
                                   'area_id'        		=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                
                                   'company_id'     	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'voltage'			  	=> array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),
                                  'machine_name'  => array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => true,
                                                              'default'     => ''),
                                   'serial_number'	=> array('type'        => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),
                                   'cycles'         		=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),
                                  'inactive'					=> array('type'         => 'boolean')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;

    // allow several entries for the same keyword here
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_model_id',
        'fields'    => array('model_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reequipment'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;   
    
        // allow several entries for the same keyword here
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_area_id',
        'fields'    => array('area_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reequipment'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;  
    
    // allow several entries for the same keyword here
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reequipment'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;      
    
    $query = xarDBCreateTable($xartable['remaintenance'],
                             array('maintenance_id'	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => true,
                                                             'primary_key'   => true),
                                   'equipment_id'   => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),
                                   'company_id'    	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                             
                                   'schedule_id'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),   
                                   'frequency_id'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'date_scheduled'	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),    
                                   'date_due'       => array('type'         => 'integer',
                                                             'null'          => true,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'cycles_due'     => array('type'         => 'integer',
                                                             'null'          => true,
                                                             'increment'     => false,
                                                             'primary_key'   => false),      
                                   'date_complete'  => array('type'         => 'integer',
                                                             'null'          => true,
                                                             'increment'     => false,
                                                             'primary_key'   => false), 
                                   'issue'         	=> array('type'        => 'text',
                                                              'default'     => ''),              
                                   'resolution'     => array('type'        => 'text',
                                                              'default'     => ''),
                                   'item_ids'       => array('type'        => 'text',
                                                              'default'     => ''),                                                                 
                                   'manhours'  		=> array('type'      => 'float',
															'size'		=> 'decimal',
															'width'		=> '10',
															'decimals'  => '1'),     
                                   'downtime'  		=> array('type'      => 'float',
															'size'		=> 'decimal',
															'width'		=> '10',
															'decimals'  => '1')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
        'fields'    => array('equipment_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['remaintenance'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;    
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['remaintenance'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;  
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_schedule_id',
        'fields'    => array('schedule_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['remaintenance'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;     
    
    $query = xarDBCreateTable($xartable['rescheduled'],
                             array('schedule_id'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => true,
                                                             'primary_key'   => true),
                                   'area_id'        => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'model_id'       => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                                                        
                                   'frequency_id'	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false), 
                                   'issue'          => array('type'        => 'text',
                                                              'default'     => ''),   
                                   'item_ids'       => array('type'        => 'text',
                                                              'default'     => ''),                                                                 
                                   'manhours'  		=> array('type'      => 'float',
                                   								'size'		=> 'decimal',
                                   								'width'		=> '10',
                                   								'decimals'  => '1'),     
                                   'downtime'  		=> array('type'         => 'boolean')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_area_id',
        'fields'    => array('area_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rescheduled'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;   
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_model_id',
        'fields'    => array('model_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rescheduled'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;          
    
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_frequency_id',
        'fields'    => array('frequency_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rescheduled'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;          
    
    $query = xarDBCreateTable($xartable['reitems'],
                             array('item_id'         => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => true,
                                                             'primary_key'   => true),
                                   'invoice_number'   => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),                                                               
                                   'part_number'     => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),  
                                   'description'     => array('type'        => 'text',
                                                              'default'     => ''), 
                                   'model_id'        => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'equipment_id'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                                                                                      
                                   'company_id'      => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                                                                                      
                                   'last_ordered'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                
                                   'man_part_number'     => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),  
                                   'cost'            => array('type'      => 'float',
                                   								'size'		=> 'decimal',
                                   								'width'		=> '10',
                                   								'decimals'  => '4')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_model_id',
        'fields'    => array('model_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reitems'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return; 
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
        'fields'    => array('equipment_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reitems'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;     
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reitems'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;     
    
    $query = xarDBCreateTable($xartable['reordered'],
                             array('order_id'         => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => true,
                                                             'primary_key'   => true),                                                             
                                   'part_number'     => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),  
                                   'description'     	=> array('type'        => 'text',
                                                              'default'     => ''), 
                                   'model_id'        	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),  
                                   'equipment_id'    => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                                                                                      
                                   'company_id'      => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                                                                                      
                                   'last_ordered'    	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),        
                                  'date_expected'  	=> array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),         
                                   'man_part_number'  => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),  
                                   'cost'           	 	=> array('type'      => 'float',
                                   								'size'		=> 'decimal',
                                   								'width'		=> '10',
                                   								'decimals'  => '4'),
                                  'agent'     			=> array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),  
                                 'tracking_number' => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),
                                 'return_auth'     	=> array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),
                                 'credit_memo'     	=> array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),                                                              
                                 'date_returned'  => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false)
                                                              
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_model_id',
        'fields'    => array('model_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reordered'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return; 
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
        'fields'    => array('equipment_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reordered'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;     
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reordered'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;     
    
    
    
    
    $query = xarDBCreateTable($xartable['reshipping'],
                             array('shipping_id' 	=> array('type'         => 'integer',
                                                            'null'          => false,
                                                            'increment'     => true,
                                                            'primary_key'   => true),
                                   'company_id'      => array('type'         => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                                              
                                   'invoice_number' => array('type'         => 'varchar',
                                                              'size'        => 254,
                                                              'null'        => false,
                                                              'default'     => ''),
                                   'cost'            => array('type'        => 'float',
                                   							  'size'		=> 'decimal',
                                   							  'width'		=> '10',
                                   							  'decimals'    => '4')
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
        'fields'    => array('company_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['reshipping'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;       
    
    $query = xarDBCreateTable($xartable['rel_cycles'],
                             array('maintenance_id'     => array('type'       => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),
                                   'item_id'        	=> array('type'       => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),   
                                   'equipment_id'       => array('type'     => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                          
                                   'cycles'    			=> array('type'       => 'integer',
                                                             'null'          => false,
                                                             'increment'     => false,
                                                             'primary_key'   => false),                                
                                   'date_added'   		=> array('type'        => 'integer',
                                                             'null'          => true,
                                                             'increment'     => false,
                                                             'primary_key'   => false)
                                  ));

    if (empty($query)) return; // throw back

    // Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_maintenance_id',
        'fields'    => array('maintenance_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rel_cycles'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;       
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
        'fields'    => array('equipment_id'),
        'unique'    => false
    );
    $query = xarDBCreateIndex($xartable['rel_cycles'],$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;        
    
    $index = array(
        'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_item_id',
        'fields'    => array('item_id'),
        'unique'    => false
    );

    
    xarRegisterMask('AdminEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADMIN');
    xarRegisterMask('EditEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADD');
    xarRegisterMask('AdminReEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADMIN');
    xarRegisterMask('EditReEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADD');
    // Initialisation successful
    return true;
}

/**
 * upgrade the recompany module from an old version
 * This function can be called multiple times
 */
function reequipment_upgrade($oldversion)
{
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    xarDBLoadTableMaintenanceAPI();
    // Upgrade dependent on old version number
    switch ($oldversion) {
        case '1.0':
        case '1.0.0':
			// Add company id index to shipping
			
			$index = array(
			'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
			'fields'    => array('company_id'),
			'unique'    => false);
			$query = xarDBCreateIndex($xartable['reshipping'],$index);
			$result =& $dbconn->Execute($query);
			if (!$result) return;       
			
			// Add Voltage to Equipment Table
            $query = xarDBAlterTable($xartable['reequipment'],
                              array('command' => 'add',
                                    'field'   => 'voltage',
                                    'type'    => 'varchar',
                                    'size'        => 255,
                                    'null'        => false,
                                    'default'     => '0'));
            $result = &$dbconn->Execute($query);
            if (!$result) return;
			
			// Add rel_frequency table
			$query = xarDBCreateTable($xartable['rel_frequency'],
					 array('company_id'     => array('type'       => 'integer',
																	'null'          => false,
																	'increment'     => false,
																	'primary_key'   => false),
						   'schedule_id'        	=> array('type'       => 'integer',
																	  'null'          => false,
																	  'increment'     => false,
																	  'primary_key'   => false),   
						   'equipment_id'        => array('type'       => 'integer',
																	  'null'          => false,
																	  'increment'     => false,
																	  'primary_key'   => false),  
						   'frequency_id'       => array('type'     => 'integer',
																	'null'          => false,
																	'increment'     => false,
																	'primary_key'   => false)
						  ));

						if (empty($query)) return; // throw back
						
						// Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
						$result = &$dbconn->Execute($query);
						if (!$result) return;
						
						$index = array(
							'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
							'fields'    => array('company_id'),
							'unique'    => false
						);
						$query = xarDBCreateIndex($xartable['rel_frequency'],$index);
						$result =& $dbconn->Execute($query);
						if (!$result) return;       
						
						$index = array(
							'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
							'fields'    => array('equipment_id'),
							'unique'    => false
						);
						$query = xarDBCreateIndex($xartable['rel_frequency'],$index);
						$result =& $dbconn->Execute($query);
						if (!$result) return;        
						
						$index = array(
							'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_schedule_id',
							'fields'    => array('schedule_id'),
							'unique'    => false
						);
						$query = xarDBCreateIndex($xartable['rel_frequency'],$index);
						$result =& $dbconn->Execute($query);
						if (!$result) return;        
						
						$index = array(
							'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_frequency_id',
							'fields'    => array('frequency_id'),
							'unique'    => false
						);
						$query = xarDBCreateIndex($xartable['rel_frequency'],$index);
						$result =& $dbconn->Execute($query);
						if (!$result) return;  
			
				xarRegisterMask('AdminEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADMIN');
				xarRegisterMask('EditEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADD');
				xarRegisterMask('AdminReEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADMIN');
				xarRegisterMask('EditReEquipment', 'All', 'reequipment', 'Item', 'All:All:All', 'ACCESS_ADD');
				
        case '1.0.1':
        	    $query = xarDBDropTable($xartable['rel_frequency']);
    			if (empty($query)) return; // throw back
    			// Drop the table and send exception if returns false.
    			$result = &$dbconn->Execute($query);
    			if (!$result) return;
        
        		// Add frequency_id to Maintenance Table
				$query = xarDBAlterTable($xartable['remaintenance'],
								  array('command' => 'add',
										'field'   => 'frequency_id',
										'type'         => 'integer',
                                        'null'          => false,
                                        'increment'     => false,
                                        'primary_key'   => false));
				$result = &$dbconn->Execute($query);
				if (!$result) return;
        
            break;
            
        case '1.0.3':

        		// Add machine name to equipment
				$query = xarDBAlterTable($xartable['reequipment'],
								  array('command' => 'add',
										'field'   => 'machine_name',
										'type'        => 'varchar',
                                        'size'        => 254,
                                        'null'        => true,
                                        'default'     => '')
                                        );
				$result = &$dbconn->Execute($query);
				if (!$result) return;
				
				// Add active to equipment
				$query = xarDBAlterTable($xartable['reequipment'],
								  array('command' => 'add',
										'field'   => 'inactive',
										'type'         => 'boolean')
                                        );
				$result = &$dbconn->Execute($query);
				if (!$result) return;
				
			
            break;   
            
            case '1.0.4':
            
				$query = xarDBCreateTable($xartable['reordered'],
										 array('order_id'         => array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => true,
																		 'primary_key'   => true),                                                             
											   'part_number'     => array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),  
											   'description'     	=> array('type'        => 'text',
																		  'default'     => ''), 
											   'model_id'        	=> array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false),  
											   'equipment_id'    => array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false),                                                                                                                      
											   'company_id'      => array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false),                                                                                                                      
											   'last_ordered'    	=> array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false),        
											  'date_expected'  	=> array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false),         
											   'man_part_number'  => array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),  
											   'cost'           	 	=> array('type'      => 'float',
																			'size'		=> 'decimal',
																			'width'		=> '10',
																			'decimals'  => '4'),
											  'agent'     			=> array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),  
											 'tracking_number' => array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),
											 'return_auth'     	=> array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),
											 'credit_memo'     	=> array('type'         => 'varchar',
																		  'size'        => 254,
																		  'null'        => false,
																		  'default'     => ''),                                                              
											 'date_returned'  => array('type'         => 'integer',
																		 'null'          => false,
																		 'increment'     => false,
																		 'primary_key'   => false)
																		  
											  ));
			
				if (empty($query)) return; // throw back
			
				// Pass the Table Create DDL to adodb to create the table and send exception if unsuccessful
				$result = &$dbconn->Execute($query);
				if (!$result) return;
				
				$index = array(
					'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_model_id',
					'fields'    => array('model_id'),
					'unique'    => false
				);
				$query = xarDBCreateIndex($xartable['reordered'],$index);
				$result =& $dbconn->Execute($query);
				if (!$result) return; 
				
				$index = array(
					'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_equipment_id',
					'fields'    => array('equipment_id'),
					'unique'    => false
				);
				$query = xarDBCreateIndex($xartable['reordered'],$index);
				$result =& $dbconn->Execute($query);
				if (!$result) return;     
				
				$index = array(
					'name'      => 'i_' . xarDBGetSiteTablePrefix() . '_company_id',
					'fields'    => array('company_id'),
					'unique'    => false
				);
				$query = xarDBCreateIndex($xartable['reordered'],$index);
				$result =& $dbconn->Execute($query);
				if (!$result) return;                 
            break;
            
    }
    // Update successful
    return true;
}
/**
 * delete the recompany module
 * This function is only ever called once during the lifetime of a particular
 * module instance
 */
function reequipment_delete()
{
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    xarDBLoadTableMaintenanceAPI();
    // Generate the SQL to drop the table using the API
    $query = xarDBDropTable($xartable['reareas']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['refrequency']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['remodels']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['reequipment']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['remaintenance']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['rescheduled']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['reitems']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['rel_eq']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['rel_cycles']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['reshipping']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['reordered']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['rel_frequency']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result = &$dbconn->Execute($query);
    if (!$result) return;
    // Remove Masks and Instances
    xarRemoveMasks('reequipment');
    xarRemoveInstances('reequipment'); 
    xarModDelAllVars('reequipment');;
    // Deletion successful
    return true;
}
?>